package com.wechat.common.web.result;

import lombok.Getter;

/**
 * 统一响应码枚举
 * 响应码设计规范：
 * - 2xx: 成功响应
 * - 4xx: 客户端错误
 * - 5xx: 服务器错误
 * - 1xxx: 业务逻辑错误
 */
@Getter
public enum ResultCode {

    // ========== 成功响应 2xx ==========
    SUCCESS(200, "操作成功"),
    CREATED(201, "创建成功"),
    ACCEPTED(202, "请求已接受"),
    NO_CONTENT(204, "无内容"),

    // ========== 客户端错误 4xx ==========
    CLIENT_ERROR(400, "客户端错误"),
    PARAM_ERROR(400, "参数错误"),
    PARAM_MISSING(400, "缺少必要参数"),
    PARAM_INVALID(400, "参数格式不正确"),

    // 认证授权相关
    UNAUTHORIZED(401, "未认证"),
    UN_AUTHORIZATION(401, "未认证"), // 保持向后兼容
    TOKEN_INVALID(401, "Token无效"),
    TOKEN_EXPIRED(401, "Token已过期"),
    LOGIN_REQUIRED(401, "请先登录"),

    FORBIDDEN(403, "无权限访问"),
    ACCESS_DENIED(403, "访问被拒绝"),
    PERMISSION_DENIED(403, "权限不足"),

    NOT_FOUND(404, "资源不存在"),
    USER_NOT_FOUND(404, "用户不存在"),
    DATA_NOT_FOUND(404, "数据不存在"),

    METHOD_NOT_ALLOWED(405, "请求方法不允许"),
    NOT_ACCEPTABLE(406, "不可接受的请求"),
    CONFLICT(409, "资源冲突"),
    GONE(410, "资源已删除"),

    REQUEST_TOO_LARGE(413, "请求体过大"),
    UNSUPPORTED_MEDIA_TYPE(415, "不支持的媒体类型"),
    TOO_MANY_REQUESTS(429, "请求过于频繁"),

    // ========== 服务器错误 5xx ==========
    SERVER_ERROR(500, "服务器内部错误"),
    INTERNAL_ERROR(500, "内部服务错误"),
    NOT_IMPLEMENTED(501, "功能未实现"),
    BAD_GATEWAY(502, "网关错误"),
    SERVICE_UNAVAILABLE(503, "服务不可用"),
    GATEWAY_TIMEOUT(504, "网关超时"),
    SYSTEM_BUSY(505, "系统繁忙"),

    // ========== 业务逻辑错误 1xxx ==========

    // 用户相关 10xx
    USER_ALREADY_EXISTS(1001, "用户已存在"),
    PHONE_ALREADY_REGISTERED(1002, "手机号已注册"),
    EMAIL_ALREADY_REGISTERED(1003, "邮箱已注册"),
    USERNAME_ALREADY_EXISTS(1004, "用户名已存在"),
    PASSWORD_ERROR(1005, "密码错误"),
    OLD_PASSWORD_ERROR(1006, "原密码错误"),
    PASSWORD_SAME_AS_OLD(1007, "新密码不能与原密码相同"),
    USER_DISABLED(1008, "用户已被禁用"),
    USER_LOCKED(1009, "用户已被锁定"),

    // 验证码相关 11xx
    VERIFICATION_CODE_ERROR(1101, "验证码错误"),
    VERIFICATION_CODE_EXPIRED(1102, "验证码已过期"),
    VERIFICATION_CODE_NOT_FOUND(1103, "验证码不存在"),
    VERIFICATION_CODE_SEND_FAILED(1104, "验证码发送失败"),
    VERIFICATION_CODE_SEND_TOO_FREQUENT(1105, "验证码发送过于频繁"),
    VERIFICATION_CODE_ERROR_TOO_MUCH(1106, "验证码错误次数过多，请稍后重试"),

    // 短信相关 12xx
    SMS_SEND_FAILED(1201, "短信发送失败"),
    SMS_TEMPLATE_ERROR(1202, "短信模板错误"),
    SMS_SIGN_ERROR(1203, "短信签名错误"),
    SMS_QUOTA_EXCEEDED(1204, "短信配额已用完"),

    // 文件相关 13xx
    FILE_NOT_FOUND(1301, "文件不存在"),
    FILE_UPLOAD_FAILED(1302, "文件上传失败"),
    FILE_SIZE_EXCEEDED(1303, "文件大小超出限制"),
    FILE_TYPE_NOT_SUPPORTED(1304, "不支持的文件类型"),
    FILE_DOWNLOAD_FAILED(1305, "文件下载失败"),
    FILE_TOO_LARGE(1306, "文件过大"),
    UNSUPPORTED_FILE_TYPE(1307, "不支持的文件类型"),
    INVALID_PARAM(1308, "参数无效"),
    UPLOAD_SESSION_NOT_FOUND(1309, "上传会话不存在"),
    CHUNK_MD5_ERROR(1310, "分片MD5校验失败"),
    FILE_MD5_ERROR(1311, "文件MD5校验失败"),
    CHUNK_INCOMPLETE(1312, "分片不完整"),
    CHUNK_FILE_MISSING(1313, "分片文件缺失"),
    OPERATION_TOO_FREQUENT(1314, "操作过于频繁"),
    OPERATION_TIMEOUT(1315, "操作超时"),

    // 数据库相关 14xx
    DATABASE_ERROR(1401, "数据库操作失败"),
    DUPLICATE_KEY_ERROR(1402, "数据重复"),
    FOREIGN_KEY_ERROR(1403, "外键约束错误"),
    DATA_INTEGRITY_ERROR(1404, "数据完整性错误"),

    // 第三方服务相关 15xx
    THIRD_PARTY_SERVICE_ERROR(1501, "第三方服务异常"),
    PAYMENT_SERVICE_ERROR(1502, "支付服务异常"),
    OAUTH_ERROR(1503, "第三方登录失败"),

    // 业务逻辑相关 16xx
    OPERATION_NOT_ALLOWED(1601, "操作不被允许"),
    BUSINESS_LOGIC_ERROR(1602, "业务逻辑错误"),
    DATA_VALIDATION_FAILED(1603, "数据验证失败"),
    DUPLICATE_OPERATION(1604, "重复操作"),

    // 聊天相关 17xx
    MESSAGE_SEND_FAILED(1701, "消息发送失败"),
    MESSAGE_NOT_FOUND(1702, "消息不存在"),
    CHAT_ROOM_NOT_FOUND(1703, "聊天室不存在"),
    CHAT_PERMISSION_DENIED(1704, "无聊天权限"),
    MESSAGE_TOO_LONG(1705, "消息内容过长"),

    // 好友相关 18xx
    FRIEND_REQUEST_ALREADY_EXISTS(1801, "好友申请已存在"),
    FRIEND_ALREADY_EXISTS(1802, "已经是好友关系"),
    FRIEND_NOT_FOUND(1803, "好友关系不存在"),
    CANNOT_ADD_SELF_AS_FRIEND(1804, "不能添加自己为好友"),
    FRIEND_REQUEST_EXPIRED(1805, "好友申请已过期"),

    // 红包相关 19xx
    RED_PACKET_NOT_FOUND(1901, "红包不存在"),
    RED_PACKET_EXPIRED(1902, "红包已过期"),
    RED_PACKET_ALREADY_RECEIVED(1903, "红包已领取"),
    RED_PACKET_INSUFFICIENT_BALANCE(1904, "余额不足"),
    RED_PACKET_CANNOT_RECEIVE_SELF(1905, "不能领取自己的红包"),

    // 朋友圈相关 20xx
    MOMENT_NOT_FOUND(2001, "动态不存在"),
    MOMENT_PERMISSION_DENIED(2002, "无权限查看该动态"),
    MOMENT_ALREADY_LIKED(2003, "已经点过赞了"),
    MOMENT_NOT_LIKED(2004, "还未点赞"),
    MOMENT_CONTENT_TOO_LONG(2005, "动态内容过长");

    private final int code;
    private final String message;

    ResultCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 根据code获取ResultCode
     */
    public static ResultCode getByCode(int code) {
        for (ResultCode resultCode : ResultCode.values()) {
            if (resultCode.getCode() == code) {
                return resultCode;
            }
        }
        return SERVER_ERROR; // 默认返回服务器错误
    }

    /**
     * 判断是否成功
     */
    public boolean isSuccess() {
        return this.code >= 200 && this.code < 300;
    }

    /**
     * 判断是否客户端错误
     */
    public boolean isClientError() {
        return this.code >= 400 && this.code < 500;
    }

    /**
     * 判断是否服务器错误
     */
    public boolean isServerError() {
        return this.code >= 500 && this.code < 600;
    }

    /**
     * 判断是否业务逻辑错误
     */
    public boolean isBusinessError() {
        return this.code >= 1000 && this.code < 10000;
    }
}
